﻿using Core.Dtos;
using Core.Entities;
using Core.IRepository;
using Core.Messages;
using Core.Queries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Front.Controllers.Api
{
    public class ColumnApiController : CrudBaseController<int, Column, ColumnDto, ColumnQuery>
    {
        IItemValueRepository DataRepository;
        public ColumnApiController(IUnitOfWork UnitOfWork, IColumnRepository ColumnRepository, IItemValueRepository DataRepository)
            : base(UnitOfWork, ColumnRepository)
        {
            this.DataRepository = DataRepository;
        }        

        [HttpPost]
        [Route("api/columnapi/findby")]
        public ListResponse<ColumnDto> FindBy(BaseRequest<ColumnQuery> request)
        {            
            return base.DefaultFindBy(request.Value);            
        }

        public BaseResponse<int> Post(BaseRequest<ColumnDto> request)
        {
            return base.DefaultAdd(request.Value);
        }

        public BaseResponse Put(int id, BaseRequest<ColumnDto> request)
        {
            return base.DefaultEdit(id, request.Value);
        }

        public override BaseResponse Delete(int id)
        {
            BaseResponse response = new BaseResponse();
            try
            {
                Column entity = Repository.FindBy(id);
                DataRepository.Delete(x => x.ColumnID == id);
                Repository.Delete(entity);
                UnitOfWork.Commit();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ErrorMessage = ex.ToString();
            }

            return response;
        }
    }
}
